//
//  PhotoAlbumViewController.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2018/12/26.
//  Copyright © 2018年 郑逸凯. All rights reserved.
//

#import "PhotoAlbumViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "AlbumCollectionViewCell.h"
#import "PhotoViewController.h"
#import "WhiteNaviBar.h"
#import "AlbumModel.h"


#define COLLECTION_MARGIN 20

@interface PhotoAlbumViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)WhiteNaviBar *naviBar;
@property (nonatomic,strong)NSMutableArray *albumList;
@property (nonatomic,strong)RequestSession *albumListRequest;
@end

@implementation PhotoAlbumViewController
#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
    [self performLayout];
    
    
    [self requestAlbumListRequest];
    @weakify(self);
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self requestAlbumListRequest];
    }];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

#pragma mark - Public Methods
#pragma mark - Responder Actions



#pragma mark - Delegates
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.albumList.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AlbumCollectionViewCell*cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumCollectionViewCell" forIndexPath:indexPath];
    if (!cell ) {
        NSLog(@"cell为空,创建cell");
        cell = [[AlbumCollectionViewCell alloc] init];
    }
    AlbumModel *model = [self.albumList tm_safeObjectAtIndex:indexPath.row];
    if (model) {
        [cell reloadCell:model];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AlbumModel *model = [self.albumList tm_safeObjectAtIndex:indexPath.row];
    if (!model) {
        [YAToast showTouchableTipMessageInView:@"查看相册出错，请尝试刷新列表"];
        return;
    }
    PhotoViewController *photoVC = [[PhotoViewController alloc] initWithModel:model];
    [self.navigationController pushViewController:photoVC animated:YES];
}

#pragma mark - Private Methods
- (void)performLayout {
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(COLLECTION_MARGIN);
        make.right.equalTo(self.view).with.offset(-COLLECTION_MARGIN);
        make.top.equalTo(self.naviBar.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
}

- (void)setupSubviews {
    self.naviBar = [WhiteNaviBar newBar];
    [self.naviBar setTitle:@"相册"];
    [self.view addSubview:self.naviBar];
    [self.view addSubview:self.collectionView];
}

- (void)requestAlbumListRequest {
    @weakify(self);
    NSString *urlStr = WO_PATH_ALBUM_LIST;
    [self.albumListRequest getWithURLString:WO_URL(urlStr) parameters:@{} completionHandler:^(id responseObject, NSError *error) {
        @strongify(self);
        if (self.collectionView.mj_header.isRefreshing) {
            [self.collectionView.mj_header endRefreshing];
        }
//        if (self.tableView.mj_footer.isRefreshing) {
//            [self.tableView.mj_footer endRefreshing];
//        }
        if (!error ) {
            NSDictionary *data = [responseObject tm_dictionaryForKey:@"data"];
            NSArray *listArray = [data tm_arrayForKey:@"albumIndexEntityList"];
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *boatDic in listArray) {
                [array addObject:[AlbumModel yy_modelWithDictionary:boatDic]];
            }
            self.albumList = array;
           
            [self.collectionView reloadData];
        }else {
           
            [YAToast showTouchableTipMessageInView:error.domain];
        }
    }];
}

#pragma mark - Setters and Getters
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        CGRect collectionViewFrame= CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 100);
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        // 设置UICollectionView为横向滚动
//        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        // 每一行cell之间的间距
        flowLayout.minimumLineSpacing = 10;
        // 每一列cell之间的间距
         flowLayout.minimumInteritemSpacing = 10;
        // 设置第一个cell和最后一个cell,与父控件之间的间距
        flowLayout.sectionInset = UIEdgeInsetsMake(20, 0, 20, 0);
        
        //    flowLayout.minimumLineSpacing = 1;// 根据需要编写
        //    flowLayout.minimumInteritemSpacing = 1;// 根据需要编写
        CGFloat itemHeight = (SCREEN_WIDTH - COLLECTION_MARGIN *2 - 10 )/2;
        flowLayout.itemSize = CGSizeMake(itemHeight, itemHeight + 40 );// 该行代码就算不写,item也会有默认尺寸
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:flowLayout];
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        [collectionView registerClass:[AlbumCollectionViewCell class] forCellWithReuseIdentifier:@"AlbumCollectionViewCell"];
        _collectionView = collectionView;
    }
    return _collectionView;
}


- (RequestSession *)albumListRequest {
    if (!_albumListRequest) {
        _albumListRequest = [[RequestSession alloc] init];
    }
    return _albumListRequest;
}

#pragma mark - Helper

@end
